Java and Flex Integration Bible by Keefe Matthew Christiansen Charles A
Author:Keefe, Matthew, Christiansen, Charles A.
Language: eng
Format: epub
Publisher: John Wiley & Sons, Inc.
Published: 2010-03-09T16:00:00+00:00
Here's the code listing for the ListAllPetsUsingHibernate class:
/**
*
*/
package com.wiley.jfib.ch09;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
/**
* @author Chuck
* The ListAllPetsUsingHibernate class retrieves all of the pets from the
* PETS table in the database and lists the name, type, and
* owner's name for each one.
*/
public class ListAllPetsUsingHibernate {
/**
* @param args Any arguments passed to this application.
* This application expects and uses no arguments.
*/
public static void main(String[] args)
{
try
{
// Set up the Hibernate session
Configuration config = new Configuration();
config.configure(“hibernate.cfg.xml”);
SessionFactory sessionFactory = config.buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
Transaction tx = session.beginTransaction();
// get the current list of pets and owners
List<Pet> allPets = session.createCriteria(Pet.class).list();
for(Pet pet : allPets)
{
// print out each pet in the list
System.out.println(pet.getName()
+ “ is a “ + pet.getType()
+ “ belonging to “
+ pet.getPerson().getFirstName() + “ “ + pet.getPerson().getLastName());
}
tx.commit();
}
catch (HibernateException e)
{
e.printStackTrace();
}
}
}
At the beginning of this class, a number of Hibernate classes are imported. Configuration is used to read the configuration from the hibernate.cfg.xml file. SessionFactory uses this configuration to create a Session class. Transaction is a class that Hibernate provides for transaction management, which is a way of grouping operations into a single unit so that they all succeed or fail as one. Finally, any errors that occur within Hibernate code result in a HibernateException being thrown.
In the main method of this class, the Hibernate session is created by the SessionFactory by using the information from the Configuration object config. A transaction is started by the session — although, in reality, a session doesn't serve much purpose here with only a single operation being run; Hibernate requires some sort of transaction management to be in place.
This line of code does the bulk of the work:
List<Pet> allPets = session.createCriteria(Pet.class).list();
The Hibernate Session object has a variety of methods for retrieving objects. For retrieving single objects whose primary key value (for example, PET_ID) is known, you would use the load() method and supply it with the class of the Pet object and the primary key value. To retrieve a list of all objects of a given class, you would use a line like the one just presented. This line says to get a list of every Pet object from the database. Hibernate goes to the database and retrieves not only all the pets, but because of the many-to-one relationship you set up in the Hibernate mapping file between Pet and Person, it also populates the Person object property in the Pet object. The type List<Pet> indicates that this List object contains only Pets. The loop takes each Pet object in the list and prints out the pet's name and type, but thanks to the many-to-one relationship, it can also print out the owner's first and last names.
To run the application, right-click the ListAllPetsUsingHibernate.java file in the Project Explorer and then choose Run As⇒Java Application from the popup menu. You should see the program's output in the Console view, as shown in Figure 9.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Deep Learning with Python by François Chollet(16098)
The Mikado Method by Ola Ellnestam Daniel Brolund(13375)
Hello! Python by Anthony Briggs(13181)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(12341)
Dependency Injection in .NET by Mark Seemann(12198)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(10955)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(10804)
A Developer's Guide to Building Resilient Cloud Applications with Azure by Hamida Rebai Trabelsi(10540)
Grails in Action by Glen Smith Peter Ledbrook(10264)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(10201)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(9552)
Hit Refresh by Satya Nadella(9040)
Kotlin in Action by Dmitry Jemerov(8930)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(8681)
The Kubernetes Operator Framework Book by Michael Dame(8488)
Exploring Deepfakes by Bryan Lyon and Matt Tora(8308)
Robo-Advisor with Python by Aki Ranin(8261)
Practical Computer Architecture with Python and ARM by Alan Clements(8234)
Implementing Enterprise Observability for Success by Manisha Agrawal and Karun Krishnannair(8203)